<template>
    <div>
        <div style="font-size:28px;margin-left: 30px;margin-top: 20px;">轮播图列表</div>
        <el-table :data="swiperList" border fit highlight-current-row>
            <el-table-column label="编号" prop="_id"></el-table-column>
            <el-table-column label="商品id" prop="goods_id"></el-table-column>
            <el-table-column label="图片">
                <template slot-scope="scope">
                    <el-image :src="scope.row.image_src"></el-image>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button @click="update(scope.row)">编辑</el-button>
                    <el-button type="danger" @click="del(scope.row._id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog :closeDelay="true" @closed="closed" title="修改轮播图" :visible.sync="openDialog" >
            <div id="head">
                <el-upload class="avatar-uploader" action="http://localhost:4000/admin/upload_temp"
                    :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                    <el-image  :src="updateForm.tempUrl" class="avatar"></el-image>
                    <!-- <i v-else class="el-icon-plus avatar-uploader-icon"></i> -->
                </el-upload>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="openDialog = false">取 消</el-button>
                <el-button type="primary" @click="yes">确定修改</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { _getAllSwiper, _deleteSwiper,_updateSwiper } from "@/api/banners"
export default {
    name: 'BannerList',
    data() {
        return {
            swiperList: [],
            openDialog: false,
            updateForm:{
                image_src:"",
                _id:"",
                goods_id:"",
                tempUrl:""
            }
        }
    },
    mounted() {
        this.getAllSwiper();
    },
    methods: {
        //上传成功
        handleAvatarSuccess(res, file) {

            // this.imageUrl = URL.createObjectURL(file.raw);
            let img = res.data.filename;
            this.updateForm.tempUrl = `http://localhost:4000/temp/${img}`
            img = `http://localhost:4000/images/${img}`;
            this.updateForm.image_src = img;
            this.$forceUpdate()
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg'||file.type === 'image/jpg' ||file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 5;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        async getAllSwiper() {
            const res = await _getAllSwiper();
            if (res.meta.status) {
                this.swiperList = res.message;
            }
        },
        async del(id) {
            console.log(id);
            const res = await _deleteSwiper({ _id: id });
            if (res.code == 200) {
                this.$message.success("删除成功")
                this.getAllSwiper()
            }
        },
        update(item) {
            this.updateForm=JSON.parse(JSON.stringify(item));
            this.updateForm.tempUrl=item.image_src;
            this.openDialog = true;
        },
        yes(){
            this.openDialog = false;
    
        },
        async closed(){
            const res=await _updateSwiper(this.updateForm)
            if(res.code==200){
                this.getAllSwiper();
                this.$message.success("修改成功")
            }
        }
       
    },
}
</script>
<style scoped>
#head /deep/ .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    margin-left: 20px;
    margin-top: 30px;
}

#head /deep/ .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

#head /deep/ .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 278px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

#head /deep/ .avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>